package com.example.mo.controller;


import com.example.mo.Entity.SaResult;
import com.example.mo.Entity.User;
import com.example.mo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //http://localhost:8082/user/findAll
    // 查询所有用户
    @RequestMapping("/findAll")
    public SaResult findAll() {
        List<User> list = userService.findAll();
        return SaResult.data(list);
    }


    //POST  http://localhost:8082/user/login
    //参数名  username 莫宗军  password 123456
    // 新增登录接口
    @PostMapping("/login")
    public SaResult login(@RequestBody Map<String, String> requestBody) {
        String username = requestBody.get("username");
        String password = requestBody.get("password");
        User user = userService.login(username, password);
        if (user != null) {
            // 不返回密码的
            user.setPassword(null);
            return SaResult.ok("登录成功").setData(user);
        }
        return SaResult.error("用户名/密码错误或账户已禁用");
    }
}

